package br.com.tarcilahelpdesk.util;

import br.com.tarcilahelpdesk.enums.*;
import br.com.tarcilahelpdesk.persistencia.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * Classe com as configurações de conexao do hibernate.
 * @author vinicius
 */
public class HibernateUtil
{
    private static final SessionFactory sessionFactory = buildSessionFactory();

    /**
     * Cria uma fabrica de sessao configurada do hibernate.
     * @return SessionFactory = fabrica de sessao configurada do hibernate.
     */
    @SuppressWarnings("unchecked")
    public static SessionFactory buildSessionFactory()
    {
        AnnotationConfiguration conf = new AnnotationConfiguration();
        //Configurar um interceptador
        //conf.setInterceptor(new Interceptador());
        SessionFactory factory = null;

        try
        {
            conf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            conf.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            conf.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/tarcilahelpdesk");
            conf.setProperty("hibernate.connection.username", "tarcilahelpdesk");
            conf.setProperty("hibernate.connection.password", "tarcilahelpdesk");
            conf.setProperty("hibernate.show_sql", "true");
            conf.setProperty("hibernate.format_sql", "true");
            conf.setProperty("hibernate.hbm2ddl.auto", "update");

            // Classes mapeadas
            conf.addAnnotatedClass(Atendente.class);
            conf.addAnnotatedClass(AtributoValor.class);
            conf.addAnnotatedClass(Chamado.class);
            conf.addAnnotatedClass(Equipe.class);           
            conf.addAnnotatedClass(Fluxo.class);
            conf.addAnnotatedClass(Servico.class);
            conf.addAnnotatedClass(Solicitante.class); 
            conf.addAnnotatedClass(FluxoEquipe.class);
            
            factory = conf.buildSessionFactory();
        }
        catch (Exception e)
        {
            System.out.println("Erro: " + e.getMessage());        
        }
        return factory;
    }

    /**
     * Pega a unica fabrica de sessao configurada do hibernate.
     * @return SessionFactory = instancia unica da fabrica de sessao do hibernate.
     */
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}

